/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panelAdmin;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author DuyDang
 */
public class ItemTableModel extends AbstractTableModel {

    private ArrayList<Item> list;
    private String[] columnnames = new String[]{"ID", "Name", "Type", "Price", "Preview Image"};

    public ItemTableModel(ArrayList<Item> list) {
        this.list = list;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return columnnames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnnames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = list.get(rowIndex);
        if (columnIndex == 0) {
            return item.getIdItem();
        } else if (columnIndex == 1) {
            return item.getNameItem();
        } else if (columnIndex == 2) {
            return item.getTypeItem();
        } else if (columnIndex == 3) {
            return item.getPrice();
        } else {
            return item.getLinkItem();
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Item item = list.get(rowIndex);
        if (columnIndex == 0) {
            item.setIdItem(aValue.toString());
        } else if (columnIndex == 1) {
            item.setNameItem(aValue.toString());
        } else if (columnIndex == 2) {
            item.setTypeItem(aValue.toString());
        } else if (columnIndex == 3) {
            item.setPrice(Integer.parseInt(aValue.toString()));
        } else {
            item.setLinkItem(aValue.toString());
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        Object o = getValueAt(0, columnIndex);
        if (o == null) {
            return Object.class;
        } else {
            return o.getClass();
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }
}
